import {
    createRouter,
    createWebHashHistory
} from 'vue-router'

import storage from '@/utils/storage.js'
const {
    getLocal
} = storage
import {
    ElMessage,
    ElMessageBox
} from 'element-plus'

const routes = [{
        name: 'work',
        path: '/',
        meta: {
            title: '记事本'
        },
        component: () => import('@/views/Msg/index.vue')
    },
    {
        name: 'login',
        path: '/login',
        meta: {
            title: '管理员登录'
        },
        component: () => import('@/views/Admin/login.vue')
    },
    {
        name: 'admin',
        path: '/admin',
        meta: {
            title: '管理员页面',
            needLogin: true
        },
        component: () => import('@/views/Admin/index.vue')
    },
    {
        name: 'check',
        path: '/check',
        meta: {
            title: '查看留言',
            needLogin: true
        },
        component: () => import('@/views/Admin/check.vue')
    },
    {
        name: '404',
        path: '/404',
        meta: {
            title: '页面不存在'
        },
        component: () => import('@/views/common/404.vue')
    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})

router.beforeEach((to, from, next) => {
    const isLogin = getLocal("17003334_user")
    if (router.hasRoute(to.name)) {
        // console.log(to.path)
        document.title = to.meta.title;
        if (!isLogin && to.meta.needLogin) {
            ElMessage.warning('请先登录！')
            next('/login')
        } else {
            next()
        }
    } else {
        next('/404')
    }
})

export default router